#!/bin/bash

# 检查 nginx 是否在运行
if ! pgrep -x "nginx" >/dev/null; then
    echo "nginx 未在运行"
    exit 0
fi

# 尝试正常停止 nginx
echo "正在停止 nginx..."
nginx -s stop
sleep 2  # 等待 nginx 完全停止

# 检查是否还在运行
if pgrep -x "nginx" >/dev/null; then
    echo "nginx 未能正常停止，是否强制终止？(y/n): "
    read force_kill
    if [ "$force_kill" = "y" ] || [ "$force_kill" = "Y" ]; then
        echo "正在强制终止 nginx..."
        pkill -9 nginx
        sleep 1
        
        # 最后检查是否已经完全停止
        if ! pgrep -x "nginx" >/dev/null; then
            echo "nginx 已成功停止"
        else
            echo "无法停止 nginx，请手动检查进程"
            exit 1
        fi
    else
        echo "取消强制终止操作"
        exit 1
    fi
else
    echo "nginx 已成功停止"
fi 